include Java

require "T:/Files/weka/dist/weka.jar"
require "string"

import "weka.core.Utils"
import "weka.classifiers.Evaluation"
import "weka.classifiers.meta.Vote"

class Component
  @im
  @last_vote_options

  def initialize( im )
    @im = im
    @last_vote_options = ""
  end

  def get_result_value( pool, num_of_ensemble_classifier_members, train, test, metric, test_case, set_of_recommended_peers = [] )
    if set_of_recommended_peers == []
      recommended_peers = PeerRankingService.get_recommended_peers( num_of_ensemble_classifier_members, test_case )
    else # user set
      recommended_peers = set_of_recommended_peers
    end

    #
    ensemble_classifier = Vote.new()
    vote_options = "-S 1 "
    ##
    case @im
    when "simple"
      recommended_peers.each { | recommended_peer_index |
        classifier_str = pool.members[ recommended_peer_index ]
        #      puts( "<classifier_str.nest>" ); puts( classifier_str.nest )
        vote_options = vote_options + "-B \"#{ classifier_str.nest }\""
        vote_options = vote_options + " "
      }
      vote_options.chop!

    when "complex_22"
      #
      vote_options = vote_options + "-B \"weka.classifiers.meta.Vote -S 1 "
      for recommended_peer_index in 0..( num_of_ensemble_classifier_members / 2 - 1 )
        classifier_str = pool.members[ recommended_peer_index ]
        vote_options = vote_options + "-B \\\"#{ classifier_str.nest.nest }\\\""
        vote_options = vote_options + " "
      end
      vote_options.chop!
      vote_options = vote_options + " -R AVG\""

      #
      vote_options = vote_options + " "

      #
      vote_options = vote_options + "-B \"weka.classifiers.meta.Vote -S 1 "
      for recommended_peer_index in ( num_of_ensemble_classifier_members / 2 )..( num_of_ensemble_classifier_members - 1 )
        classifier_str = pool.members[ recommended_peer_index ]
        vote_options = vote_options + "-B \\\"#{ classifier_str.nest.nest }\\\""
        vote_options = vote_options + " "
      end
      vote_options.chop!
      vote_options = vote_options + " -R AVG\""

    when "complex_1234"
      current_chunk_size = 1
      recommended_peer_index = 0

      while ( recommended_peer_index + current_chunk_size ) <= num_of_ensemble_classifier_members
        puts( "<recommended_peer_index, current_chunk_size>" ); puts( recommended_peer_index ); puts( current_chunk_size )
        #
        vote_options = vote_options + "-B \"weka.classifiers.meta.Vote -S 1 "
        current_chunk_size.times {
          classifier_str = pool.members[ recommended_peer_index ]
          vote_options = vote_options + "-B \\\"#{ classifier_str.nest.nest }\\\""
          vote_options = vote_options + " "
          recommended_peer_index = recommended_peer_index + 1
        }
        vote_options.chop!
        vote_options = vote_options + " -R AVG\""

        #
        vote_options = vote_options + " "

        #
        current_chunk_size = current_chunk_size + 1
      end

    end
    ##
    vote_options = vote_options + " -R AVG"
    @last_vote_options = vote_options
    puts( "<1 Vote string>" ); puts( "weka.classifiers.meta.Vote #{vote_options}" )
    ensemble_classifier.setOptions( Utils.splitOptions( vote_options ) )
    ensemble_classifier.buildClassifier( train )
    #

    eval = Evaluation.new( train )
    eval.evaluateModel( ensemble_classifier, test )

    case metric
    when "pctCorrect()"
      return eval.pctCorrect()
    when "weightedAreaUnderROC()"
      return eval.weightedAreaUnderROC()
    when "weightedFalseNegativeRate()"
      return eval.weightedFalseNegativeRate()
    when "weightedFalsePositiveRate()"
      return eval.weightedFalsePositiveRate()
    when "weightedFMeasure()"
      return eval.weightedFMeasure()
    when "weightedPrecision()"
      return eval.weightedPrecision()
    when "weightedRecall()"
      return eval.weightedRecall()
    when "weightedTrueNegativeRate()"
      return eval.weightedTrueNegativeRate()
    when "weightedTruePositiveRate()"
      return eval.weightedTruePositiveRate()
    end
  end

end

